package com.xyauto.uilib.colorpicker;


import android.annotation.DrawableRes;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;


import com.xyauto.uilib.R;


/**
 * created by gongziyi on 2023/10/24 14:49
 */
public class BitColorImageView extends ImageView implements IBitmapColor {

    private static final String TAG = "BitColorImageView";
    private static final int INVALID_ID = 0;


    // 渐变角度 0~360
    private final int mAngle;
    // 是否支持src变色 默认打开
    private final boolean isSupportSrc;
    // 是否支持background变色 默认关闭
    private final boolean isSupportBackground;
    // 默认变色层
    private final int defaultLayer;

    private final boolean isInstantly;


    // src状态限制 选中状态\点击状态支持 默认空（全状态换色）、有值时（仅支持值内属性换色）
    private int[] supportSrcState;
    // background 选中状态\点击状态支持 默认空（全状态换色）、有值时（仅支持值内属性换色）
    private int[] supportBackgroundState;


    private final BitColor bitColor;
    private int srcResId;
    private int bgResId;


    public BitColorImageView(Context context, Builder builder, String tag) {
        super(context);
        this.mAngle = builder.mAngle;
        this.isSupportSrc = builder.isSupportSrc;
        this.isSupportBackground = builder.isSupportBackground;
        this.defaultLayer = builder.defaultLayer;

        bitColor = new BitColor(context, builder.isList);
        if (builder.onlySrcClick) {
            supportSrcState = new int[]{android.R.attr.state_pressed};
        }
        if (builder.onlyBackgroundClick) {
            supportBackgroundState = new int[]{android.R.attr.state_pressed};
        }
        isInstantly = false;
    }

    public BitColorImageView(Context context) {
        this(context, null);
    }

    public BitColorImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BitColorImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BitColorImageView);
        bitColor = new BitColor(context, a.getBoolean(R.styleable.BitColorImageView_isList, false),a.getBoolean(R.styleable.BitColorImageView_supportGradient,false));
        isSupportBackground = a.getBoolean(R.styleable.BitColorImageView_supportBackground, false);
        isSupportSrc = a.getBoolean(R.styleable.BitColorImageView_supportSrc, true);
        mAngle = a.getInteger(R.styleable.BitColorImageView_gradientAngle, 0);
        defaultLayer = a.getInteger(R.styleable.BitColorImageView_defaultLayer, 0);
        isInstantly = a.getBoolean(R.styleable.BitColorImageView_isInstantly, false);
        boolean onlySrcClick = a.getBoolean(R.styleable.BitColorImageView_onlySrcClick, false);
        boolean onlyBackgroundClick = a.getBoolean(R.styleable.BitColorImageView_onlyBackgroundClick, false);
        if (a.hasValue(R.styleable.BitColorImageView_android_background)) {
            bgResId = a.getResourceId(R.styleable.BitColorImageView_android_background, INVALID_ID);
        }
        if (a.hasValue(R.styleable.BitColorImageView_android_src)) {
            srcResId = a.getResourceId(R.styleable.BitColorImageView_android_src, INVALID_ID);
        }
        a.recycle();
        if (onlySrcClick) {
            supportSrcState = new int[]{android.R.attr.state_pressed, android.R.attr.state_selected};
        }
        if (onlyBackgroundClick) {
            supportBackgroundState = new int[]{android.R.attr.state_pressed, android.R.attr.state_selected};
        }
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        changeColor(BitmapColorHelper.findColour());
        BitmapColorHelper.getInstance().registerContentObserver(this, isInstantly);
    }

    @Override
    protected void onDetachedFromWindow() {
        BitmapColorHelper.getInstance().unregisterContentObserver(this);
        super.onDetachedFromWindow();
    }

    @Override
    public void setBackgroundResource(@DrawableRes int resId) {
        bgResId = resId;
        //ViewCompat.setBackground(this, ContextCompat.getDrawable(getContext(), resId));
        if (resId!=0){
            setBackgroundDrawable(getSkinDrawable(resId));
            // ViewCompat.setBackground(this, ContextCompat.getDrawable(getContext(), resId));
        }else {
            super.setBackgroundResource(0);
        }
    }

    @Override
    public void setImageResource(@DrawableRes int resId) {
        srcResId = resId;
       // setImageDrawable(ContextCompat.getDrawable(getContext(), resId));
        if (resId!=0){
            setImageDrawable(getSkinDrawable(resId));
        }else {
            super.setImageResource(0);
        }
    }


    public void applySkin() {
        if (bgResId != INVALID_ID) {
            //ViewCompat.setBackground(this, ContextCompat.getDrawable(getContext(), bgResId));
            setBackgroundDrawable(getSkinDrawable(bgResId));
        }
        if (srcResId != INVALID_ID) {
            //setImageDrawable(ContextCompat.getDrawable(getContext(), srcResId));
            setImageDrawable(getSkinDrawable(srcResId));
        }
    }

    private Drawable getSkinDrawable(int res) {
        return   BitmapColorHelper.getInstance().loadResDrawable(getContext(), res);
    }

    @Override
    public void changeColor(ColorParams params) {
        if (bitColor.loadColorParams(params)) {
            Log.i(TAG, "changeColor: " + this + ";" + params);
            applySkin();
        }
    }

    @Override
    public void setImageDrawable(@Nullable Drawable drawable) {
        if (isSupportSrc && bitColor.hasColorParams()) {
            drawable = bitColor.createDrawable(drawable, defaultLayer, mAngle, null, supportSrcState);
        }
        super.setImageDrawable(drawable);
    }


    @Override
    public void setBackground(Drawable background) {
        if (isSupportBackground && bitColor.hasColorParams()) {
            background = bitColor.createDrawable(background, defaultLayer, mAngle, null, supportBackgroundState);
        }
        super.setBackground(background);
    }

    @Override
    public void setBackgroundDrawable(Drawable background) {
        if (isSupportBackground && bitColor.hasColorParams()) {
            background = bitColor.createDrawable(background, defaultLayer, mAngle, null, supportBackgroundState);
        }
        super.setBackgroundDrawable(background);
    }


    public static class Builder {
        private int mAngle = 0;
        private boolean isSupportSrc = true;
        private boolean isSupportBackground = false;
        private int defaultLayer = 0;
        private boolean isList = false;
        private boolean onlySrcClick = false;
        private boolean onlyBackgroundClick = false;


        public Builder setAngle(int mAngle) {
            this.mAngle = mAngle;
            return this;
        }

        public Builder setSupportSrc(boolean supportSrc) {
            isSupportSrc = supportSrc;
            return this;
        }

        public Builder setSupportBackground(boolean supportBackground) {
            isSupportBackground = supportBackground;
            return this;
        }

        public Builder setDefaultLayer(int defaultLayer) {
            this.defaultLayer = defaultLayer;
            return this;
        }

        public Builder setList(boolean list) {
            isList = list;
            return this;
        }

        public Builder setOnlySrcClick(boolean onlySrcClick) {
            this.onlySrcClick = onlySrcClick;
            return this;
        }

        public Builder setOnlyBackgroundClick(boolean onlyBackgroundClick) {
            this.onlyBackgroundClick = onlyBackgroundClick;
            return this;
        }

        public BitColorImageView build(Context context) {
            return new BitColorImageView(context, this, "build");
        }
    }
}
